package com.api.gbhg.lbj.action;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import weaver.general.TimeUtil;
import weaver.general.Util;
import weaver.interfaces.schedule.BaseCronJob;
import weaver.workflow.webservices.WorkflowBaseInfo;
import weaver.workflow.webservices.WorkflowDetailTableInfo;
import weaver.workflow.webservices.WorkflowMainTableInfo;
import weaver.workflow.webservices.WorkflowRequestInfo;
import weaver.workflow.webservices.WorkflowRequestTableField;
import weaver.workflow.webservices.WorkflowRequestTableRecord;
import weaver.workflow.webservices.WorkflowServiceImpl;
/**
 * @author lbj
 * @version 1.0
 * @description: TODO
 * @date 2021/1/8 17:26
 */
public class TestAction extends BaseCronJob {
    private static final Logger logger = LoggerFactory.getLogger(TestAction.class);

    @Override
    public void execute() {
        logger.info("定时任务测试-流程创建");
        String wfid="4";//要创建的流程id
        String wfcreateid="1";//流程创建用户,1系统管理员，其他的就是hrmresource表的id
        String wflevel="0";//流程级别，正常，紧急，
        String wftitle="lbj测试 "+TimeUtil.getCurrentTimeString();//流程标题
        WorkflowRequestTableRecord[] wrtri=null;
        //--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
        //封装主表字段信息的集合--start
        wrtri =  new  WorkflowRequestTableRecord[1];
        //包装主表字段的对象--start
        WorkflowRequestTableRecord workflowRequestTableRecord = new WorkflowRequestTableRecord();
        WorkflowRequestTableField[] wrti= new WorkflowRequestTableField[2]; //字段信息-------------------------------------动态计算

        wrti[0] = new WorkflowRequestTableField();
        wrti[0].setFieldName("gysbh");
        wrti[0].setFieldValue("1111");//------------------字段的值要来自于sap的参数
        wrti[0].setView(true);//字段是否可见
        wrti[0].setEdit(true);//字段是否可编辑

        wrti[1] = new WorkflowRequestTableField();
        wrti[1].setFieldName("yhzh");
        wrti[1].setFieldValue("18");//------------------字段的值要来自于sap的参数
        wrti[1].setView(true);//字段是否可见
        wrti[1].setEdit(true);//字段是否可编辑




        workflowRequestTableRecord.setWorkflowRequestTableFields(wrti);
        wrtri[0]=workflowRequestTableRecord;


        WorkflowMainTableInfo wmi = new WorkflowMainTableInfo();
        wmi.setRequestRecords(wrtri);
        WorkflowBaseInfo wbi = new WorkflowBaseInfo();
        wbi.setWorkflowId(wfid);
        WorkflowRequestInfo wri = new WorkflowRequestInfo();//流程基本信息
        wri.setCreatorId(wfcreateid);//创建人id
        wri.setRequestLevel(wflevel);//0 正常，1重要，2紧急
        wri.setRequestName(wftitle);//流程标题
        wri.setWorkflowMainTableInfo(wmi);//添加主字段数据
        wri.setWorkflowBaseInfo(wbi);

        WorkflowDetailTableInfo WorkflowDetailTableInfo[]= new WorkflowDetailTableInfo[1];//明细表的个数


        WorkflowRequestTableRecord[] wrtrimx=new  WorkflowRequestTableRecord[2];//表示插入到明细表的总行数数据

        //包装明细字段的对象--start
        WorkflowRequestTableRecord workflowRequestTableRecordmx = new WorkflowRequestTableRecord();


        WorkflowRequestTableField[] wrtimx= new WorkflowRequestTableField[3]; //字段信息
        wrtimx[0] = new WorkflowRequestTableField();
        wrtimx[0].setFieldName("khh");//短文本
        wrtimx[0].setFieldValue("12311111");//------------------字段的值要来自于sap
        wrtimx[0].setView(true);//字段是否可见
        wrtimx[0].setEdit(true);//字段是否可编辑


        wrtimx[1] = new WorkflowRequestTableField();
        wrtimx[1].setFieldName("gys");//短文本
        wrtimx[1].setFieldValue("lbj测试1");//------------------字段的值要来自于sap
        wrtimx[1].setView(true);//字段是否可见
        wrtimx[1].setEdit(true);//字段是否可编辑



        workflowRequestTableRecordmx.setWorkflowRequestTableFields(wrtimx);
        //包装明细字段的对象--end
        wrtrimx[0]=workflowRequestTableRecordmx;//明细第n行的数据

        wrtrimx[1]=workflowRequestTableRecordmx;//明细第n行的数据


        //封装明细字段信息的集合--end
        WorkflowDetailTableInfo[0] = new WorkflowDetailTableInfo();
        WorkflowDetailTableInfo[0].setWorkflowRequestTableRecords(wrtrimx);


        if(WorkflowDetailTableInfo.length>0){
            wri.setWorkflowDetailTableInfos(WorkflowDetailTableInfo);//添加明细数据
        }
        WorkflowServiceImpl workflowServiceImpl = new WorkflowServiceImpl();
        /*-1：保存流程的基本信息出错
        -2：没有权限
        -4：数据插入到OA表单失败
        -7：流程的下一节点找不到操作者
        -8：节点自动赋值出错*/
        //<0认为创建不成功
        //流程实列和用户映射表
        //id,userid,requestid,is(0,表示已处理，1待处理)
        String requestid = workflowServiceImpl.doCreateWorkflowRequest(wri, Util.getIntValue(wfcreateid));
        logger.info("-流程创建= "+requestid);
    }
}
